<template>
	<view class="data-report">
		<view class="top-box">
			<view class="top-wrap">
				<view class="title-box">费用概况</view>
				<view class="item-box">
					<u-grid :col="3" :border="false">
						<u-grid-item>
							<view class="grid-num">{{allnumber}}</view>
							<view class="grid-text">项目数量</view>
						</u-grid-item>
						<u-grid-item>
							<view class="grid-num">{{cost}}</view>
							<view class="grid-text">总费用</view>
						</u-grid-item>
						<u-grid-item>
							<view class="grid-num">{{total}}</view>
							<view class="grid-text">总次数</view>
						</u-grid-item>
					</u-grid>
				</view>
			</view>
		</view>
		<view class="report-box">
			<!-- <view class="report-title">
				<u-divider>
					<view class="lins">
						<view class="report-date" @click="timeSelect(true)">
							<text v-if="time">{{time}}</text>
							<u-icon v-if="!time" class="arrow-down" name="arrow-down"></u-icon>
						</view>
						到
						<view class="report-date"  @click="timeSelect(false)">
							<text v-if="timeone">{{timeone}}</text>
							<u-icon v-if="!timeone" class="arrow-down" name="arrow-down"></u-icon>
						</view>
					</view>

				</u-divider>
			</view> -->
			<!-- <view class="report-info">
				<u-grid :col="2" :border="false">
					<u-grid-item>
						<view class="report-money">
							--
						</view>
						<view class="report-text">运输费用(元)</view>
					</u-grid-item>
					<u-grid-item>
						<view class="report-num">
							<u-count-to :start-val="0" :end-val="totalone"></u-count-to>
						</view>
						<view class="report-text">运输次数</view>
					</u-grid-item>
				</u-grid>
			</view> -->
			<view class="table-box">
				<view class="title">
					<u-row>
						<u-col span="4">
							<view class="title-name">项目名称</view>
						</u-col>
						<u-col span="3">
							<view class="title-name">运输次数</view>
						</u-col>
						<u-col span="3">
							<view class="title-name">合计费用</view>
						</u-col>
						<u-col span="2">
							<view class="title-name">操作</view>
						</u-col>
					</u-row>
				</view>
				<view class="table-data">
					<u-divider v-if="tableData.length === 0" class="no-data" bg-color="#eaf1ff">暂无数据</u-divider>
					<view class="data-box">
						<u-row class="data-wrap" v-for="(item, index) in tableData" :key="index" :class="[index % 2 == 1 ? 'singular':'dual']">
							<u-col span="4">
								<view class="data-item">{{ item.name }}</view>
							</u-col>
							<u-col span="3">
								<view class="data-item">{{ item.count }}</view>
							</u-col>
							<u-col span="3">
								<view class="data-item">{{ item.cost }}</view>
							</u-col>
							<u-col span="2">
								<view class="data-item data-details" @click="openPage(item.name , item.projectId)">查看详情</view>
							</u-col>
						</u-row>
					</view>
				</view>
			</view>
		</view>
<!-- 		<view class="export-excel">
			<u-button type="primary">查阅明细</u-button>
		</view> -->
		<!-- <u-picker mode="time" v-model="timeShow" :params="params" @confirm="confirm"></u-picker> -->
		<u-picker mode="time" v-model="timeShow" :params="params" @confirm="confirm"></u-picker>
		<u-picker mode="time" v-model="timeShowone" :params="params" @confirm="confirmone"></u-picker>
		<kModel ref="kModel" />
	</view>
</template>

<script>
	import {
		getReportData
	} from "@/api/dataReport.js"
	export default {
		data() {
			return {
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				allnumber: null,
				timeShow: false,
				timeShowone: false,
				form: {
					action: 'total',
				},
				formAgin: {
					action: "count",
					group: "projectId"
				},
				total: null,
				totalone: 0,
				time: null,
				timeone: null,
				tableData: [],
				cost: 0,
			};
		},
		components: {},
		created() {
			this.getReport()
			this.getReportList()
		},
		mounted() {
			this.isloading(true)
		},
		methods: {
			openPage(name , id) {
				uni.navigateTo({
					url: "./projectData/index?name=" + name + "&id=" + id
				});
			},
			isloading(status){
				if(status){
					this.$refs.kModel.showModel({
						type: 'loading',
						title: '获取数据中',
						style: 'circle',
					})
				} else {
					this.$refs.kModel.closeModel()
				}
			},
			getReport() {
				getReportData(this.form).then(res => {
					this.total = parseInt(res.result.total)
				})
			},
			onSelected(e) { //选择
				this.showPicker = false;
				if (e) {
					this[this.type] = e.value;
					//选择的值
					console.log('value => ' + e.value);
					//原始的Date对象
					console.log('date => ' + e.date);
				}
			},
			timeSelect(type) {
				if(type){
					this.timeShow = true
				} else {
					this.timeShowone = true
				}
			},
			getReportList() {
				getReportData(this.formAgin).then(res => {
					if(!this.allnumber){
						this.allnumber = res.result.length
					}
					res.result.forEach(item => {
						item.count = parseInt(item.count)
						this.cost += item.cost
					})
					this.totalone = res.result.reduce((total, item) => {
						return total + item.count
					}, 0)
					this.tableData = res.result
					this.isloading(false)
				})
				// console.log(this.formAgin)
			},
			panduan() {
				if (!!this.time && !!this.timeone) {
					if (this.formAgin.startTime >= this.formAgin.endTime) {
						uni.showToast({
							title: "开始时间不能大于结束时间，请重新选择",
							icon: "none"
						})
						this.time = this.timeone = null
						return
					}
					this.getReportList()
				}
			},
			confirm(res) {
				this.time = `${res.year}年${res.month}月${res.day}日`;
				let date = new Date(`${res.year}-${res.month}-${res.day}`).getTime()
				this.formAgin.startTime = date
				this.panduan()
				// console.log(date)
			},
			confirmone(res) {
				this.timeone = `${res.year}年${res.month}月${res.day}日`
				let date = new Date(`${res.year}-${res.month}-${res.day}`).getTime()
				this.formAgin.endTime = date
				this.panduan()
			}
		}
	};
</script>

<style lang="scss" scoped>
	.top-box {
		.top-wrap {
			background-color: #ffffff;
			border-radius: 10rpx;
			padding: 10rpx;
		}

		.title-box {
			font-size: 30rpx;
			font-weight: normal;
			font-stretch: normal;
			line-height: 70rpx;
			letter-spacing: 0rpx;
			color: #5c92ff;
			font-weight: bold;
			height: 110rpx;
			padding: 20rpx 60rpx;
			position: relative;
			border-bottom: 1px solid rgb(239, 239, 239);
		}

		.title-box::before {
			content: '';
			position: absolute;
			width: 8rpx;
			height: 40rpx;
			background-color: #007aff;
			left: 40rpx;
			top: 35rpx;
		}

		.grid-num {
			font-weight: bold;
			font-size: 36rpx;
			font-weight: normal;
			font-stretch: normal;
			letter-spacing: 0px;
			color: #2c3240;
		}

		.grid-text {
			font-size: 24rpx;
			font-weight: normal;
			font-stretch: normal;
			letter-spacing: 0px;
			color: #646c7f;
			font-weight: bold;
		}
	}

	.report-box {
		margin-top: 20rpx;

		.report-title {
			background-color: #ffffff;
			padding-top: 20rpx;

			.lins {
				width: 550rpx;
				display: flex;
				justify-content: space-around;
				align-items: center;
			}

			.report-date {
				width: 190rpx;
				height: 60rpx;
				background-color: #5c92ff;
				border-radius: 30rpx;
				color: #ffffff;
				line-height: 60rpx;

				.arrow-down {
					// margin-left: 20rpx;
				}
			}
		}

		.report-money {
			font-weight: bold;
			font-size: 48rpx;
			font-weight: normal;
			font-stretch: normal;
			letter-spacing: 0px;
			color: #ff4040;
		}

		.report-num {
			font-weight: bold;
			font-size: 48rpx;
			font-weight: normal;
			font-stretch: normal;
			letter-spacing: 0px;
			color: #2c3240;
		}

		.report-text {
			font-size: 24rpx;
			font-weight: normal;
			font-stretch: normal;
			letter-spacing: 0px;
			color: #646c7f;
			font-weight: bold;
		}

		.table-box {
			.title {
				height: 80rpx;
				background-color: #5c92ff;
				line-height: 80rpx;
				color: #ffffff;

				.title-name {
					text-align: center;
				}
			}
		}

		.table-data {
			margin-bottom: 130rpx;

			.data-item {
				text-align: center;
				// border-right: 1px solid #ccc;
				padding: 20rpx 0rpx;
				font-size: 24rpx;
				font-weight: normal;
				font-stretch: normal;
				letter-spacing: 0px;
				color: #2c3240;
			}

			.singular {
				background-color: #eaf1ff;
			}

			.dual {
				background-color: #ffffff;
			}

			.data-details {
				font-size: 24rpx;
				font-weight: normal;
				font-stretch: normal;
				letter-spacing: 0px;
				color: #5c92ff;
			}

			.no-data {
				padding-top: 40rpx;
			}
		}
	}

	.export-excel {
		position: fixed;
		width: 100%;
		bottom: 200rpx;
		padding: 0 30rpx;
		z-index: 99;
	}
	.data-wrap{
		height: 90rpx;
	}
</style>
